package com.njtoyo.taxi.admin.rest.controller.business.driver;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njtoyo.taxi.admin.aop.PermissionScope;
import com.njtoyo.taxi.admin.rest.presenter.business.driver.DriverAuditInfoDetailsPresenter;
import com.njtoyo.taxi.admin.rest.presenter.business.driver.DriverAuditPagePresenter;
import com.njtoyo.taxi.admin.rest.wrapper.business.driver.*;
import com.njtoyo.taxi.admin.service.business.driver.DriverRegistrationInfoService;
import com.njtoyo.taxi.entity.backend.AdminUser;
import com.taxi.entity.common.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

@RestController
@RequestMapping("business/registration-info")
@Api(tags = {"司机自主注册审核管理"})
public class DriverRegistrationInfoController {
    @Autowired
    private DriverRegistrationInfoService driverRegistrationInfoService;

    @GetMapping("")
    @ApiOperation(value = "司机自主注册、证件更新审核列表", notes = "司机自主注册、证件更新审核列表，权限：driver_register_audit、driver_audit_certificate_list")
    @PermissionScope(names = {"driver_register_audit", "driver_audit_certificate_list"})
    RestResult<Page<DriverAuditPagePresenter>> getPage(@ModelAttribute("admin") AdminUser adminUser,
                                                       @Valid DriverAuditQueryWrapper wrapper) {
        return driverRegistrationInfoService.getPage(adminUser, wrapper);
    }

    @GetMapping("/{auditId}")
    @ApiOperation(value = "司机自主注册、证件更新审核详情", notes = "司机自主注册、证件更新审核详情，权限：driver_register_audit、driver_audit_certificate_list")
    @PermissionScope(names = {"driver_register_audit", "driver_audit_certificate_list"})
    RestResult<DriverAuditInfoDetailsPresenter> details(@PathVariable("auditId") Long id) {
        return driverRegistrationInfoService.details(id);
    }

    @PatchMapping("/{auditId}")
    @ApiOperation(value = "司机自主注册审核修改信息", notes = "司机自主注册审核修改信息，权限：driver_register_audit_update")
    @PermissionScope(names = {"driver_register_audit_update"})
    RestResult update(@PathVariable("auditId") Long id,
                      @Valid @RequestBody DriverRegistrationInfoUpdateWrapper wrapper) {
        return driverRegistrationInfoService.update(id, wrapper);
    }

    @PatchMapping("/{auditId}/examine")
    @ApiOperation(value = "司机自主注册审核通过拒绝", notes = "司机自主注册审核通过拒绝 ，权限：driver_register_audit_agree,driver_register_audit_reject")
    @PermissionScope(names = {"driver_register_audit_agree", "driver_register_audit_reject"})
    RestResult examine(@ModelAttribute("admin") AdminUser adminUser, @PathVariable("auditId") Long id,
                       @Valid @RequestBody DriverRegistrationInfoExamineWrapper wrapper) throws UnsupportedEncodingException {
        return driverRegistrationInfoService.examine(adminUser, id, wrapper);
    }

    @PatchMapping("/{auditId}/reject")
    @ApiOperation(value = "司机自主注册审核驳回", notes = "司机自主注册审核驳回，权限：driver_register_audit_fail")
    @PermissionScope(names = {"driver_register_audit_fail"})
    RestResult reject(@ModelAttribute("admin") AdminUser adminUser, @PathVariable("auditId") Long id,
                      @Valid @RequestBody DriverRegistrationInfoExamineWrapper wrapper) throws UnsupportedEncodingException {
        return driverRegistrationInfoService.reject(adminUser, id, wrapper);
    }

    @PatchMapping("/{auditId}/check")
    @ApiOperation(value = "校验审核信息", notes = "校验审核信息，权限：driver_register_audit_check")
    @PermissionScope(names = {"driver_register_audit_check"})
    RestResult check(@PathVariable("auditId") Long id) {
        return driverRegistrationInfoService.check(id);
    }

    @GetMapping("/export")
    @ApiOperation(value = "司机自主注册审核列表 导出", notes = "司机自主注册审核列表 导出，权限：driver_register_audit")
    @PermissionScope(names = {"driver_register_audit"})
    public void exportDriver(@ModelAttribute("admin") AdminUser adminUser,
                             @Valid DriverRegistrationInfoExportWrapper wrapper, HttpServletResponse response) throws IOException {
        driverRegistrationInfoService.export(adminUser, wrapper, response);
    }

    @PatchMapping("/audit-certificate/{auditId}")
    @ApiOperation(value = "证件更新-审核", notes = "证件更新-审核，权限：driver_audit_certificate")
    @PermissionScope(names = {"driver_audit_certificate"})
    RestResult<Boolean> updateRefuse(@ModelAttribute("admin") AdminUser adminUser, @PathVariable("auditId") Long id, @Valid @RequestBody DriverAuditCertificateWrapper req) {
        return driverRegistrationInfoService.auditCertificate(adminUser, id, req);
    }

}
